package com.rustic.server.service.impl;

import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.json.JSONUtil;
import cn.xy.commons.enums.YesNoStatus;
import cn.xy.commons.exception.JzRuntimeException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rustic.server.dao.UserWeixinDao;
import com.rustic.server.enums.UserWeixinType;
import com.rustic.server.po.UserWeixin;
import com.rustic.server.service.UserWeixinService;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * UserWeixin对应的服务类实现
 *
 * @author auto
 */

@Service
public class UserWeixinServiceImpl extends ServiceImpl<UserWeixinDao, UserWeixin> implements UserWeixinService {

    @Override
    public List<UserWeixin> listByIdIn(List<Integer> idList) {
        if (CollUtil.isEmpty(idList)) {
            return new ArrayList<>();
        }
        return this.listByIds(idList);
    }

    @Override
    public Map<Integer, UserWeixin> mapByIdIn(List<Integer> idList) {
        List<UserWeixin> userWeixinList = this.listByIdIn(idList);
        return userWeixinList.stream().collect(Collectors.toMap(UserWeixin::getId, b -> b));
    }

    @Override
    public UserWeixin getByUserId(int userId) {
        LambdaQueryWrapper<UserWeixin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserWeixin::getUserId, userId);
        return this.getOne(queryWrapper);
    }

    @Override
    public UserWeixin getByUnionId(String unionId) {
        return this.getOne(new LambdaQueryWrapper<UserWeixin>()
                .eq(UserWeixin::getUnionId, unionId)
                .last("limit 1")
        );
    }

    @Override
    public UserWeixin getByOpenId(String appId, String openId) {
        return this.getOne(new LambdaQueryWrapper<UserWeixin>()
                .eq(UserWeixin::getAppId, appId)
                .eq(UserWeixin::getOpenId, openId)
        );
    }

    @Override
    public void add(Integer userId, UserWeixinType userWeixinType, String unionId, String openid, String appId) {
        UserWeixin userWeixin = new UserWeixin();
        userWeixin.setAppId(appId);
        userWeixin.setOpenId(openid);
        userWeixin.setUnionId(unionId);
        userWeixin.setUserId(userId);
        userWeixin.setType(userWeixinType.getValue());
        userWeixin.setIsBind(YesNoStatus.NO.getValue());
        userWeixin.setData("");
        this.save(userWeixin);
    }

    @Override
    public void tryAdd(int userId, String appId, WxMaUserInfo wxMaUserInfo, String openId) {
        UserWeixin userWeixin = new UserWeixin();
        userWeixin.setUserId(userId);
        userWeixin.setType(UserWeixinType.MINI_PROGRAM.getValue());
        userWeixin.setAppId(appId);
        this.setWeixinInfo(userWeixin, wxMaUserInfo, openId);
        this.save(userWeixin);
    }

    @Override
    public void tryAdd(Integer userId, String appId, WxOAuth2UserInfo wxOauth2UserInfo) {
        UserWeixin userWeixin = new UserWeixin();
        userWeixin.setUserId(userId);
        userWeixin.setType(UserWeixinType.OFFICE.getValue());
        userWeixin.setAppId(appId);
        this.setWeixinInfo(userWeixin, wxOauth2UserInfo);
        this.save(userWeixin);
    }

    private void setWeixinInfo(UserWeixin userWeixin, WxMaUserInfo wxMaUserInfo, String openId) {
        if (CharSequenceUtil.isEmpty(openId) && CharSequenceUtil.isEmpty(wxMaUserInfo.getUnionId())) {
            throw new JzRuntimeException("微信登录用户信息获取失败");
        }
        userWeixin.setOpenId(openId);
        userWeixin.setUnionId(wxMaUserInfo.getUnionId());
        userWeixin.setNickname(wxMaUserInfo.getNickName());
        userWeixin.setAvatar(wxMaUserInfo.getAvatarUrl());
//        userWeixin.setIsSubscribe(YesNoStatus.NO.getValue());
        userWeixin.setIsBind(YesNoStatus.YES.getValue());
        userWeixin.setData(JSONUtil.toJsonStr(wxMaUserInfo));
    }

    private void setWeixinInfo(UserWeixin userWeixin, WxOAuth2UserInfo wxOauth2UserInfo) {
        if (CharSequenceUtil.isEmpty(wxOauth2UserInfo.getOpenid()) && CharSequenceUtil.isEmpty(wxOauth2UserInfo.getUnionId())) {
            throw new JzRuntimeException("微信登录用户信息获取失败");
        }
        userWeixin.setOpenId(wxOauth2UserInfo.getOpenid());
        userWeixin.setUnionId(wxOauth2UserInfo.getUnionId());
        userWeixin.setNickname(wxOauth2UserInfo.getNickname());
        userWeixin.setAvatar(wxOauth2UserInfo.getHeadImgUrl());
//        userWeixin.setIsSubscribe(YesNoStatus.NO.getValue());
        userWeixin.setIsBind(YesNoStatus.YES.getValue());
        userWeixin.setData(JSONUtil.toJsonStr(wxOauth2UserInfo));
    }
}
